package com.jysz.certificate.service.pm;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.pm.PmUserDao;
import com.jysz.certificate.persistence.dao.pm.PmUserListDao;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.enums.pm.PmUserStype;
import com.jysz.certificate.persistence.model.pm.PmUser;
import com.jysz.certificate.persistence.model.pm.PmUserList;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.system.OrganizationService;
import com.jysz.certificate.service.system.ShopService;
import com.jysz.certificate.service.system.UserService;
import com.jysz.certificate.utils.DateUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class PmUserListService  extends EntityService<PmUserList ,Long,PmUserListDao>{

	@Resource
	public PmUserListDao pmUserListDao;
	@Resource
	public PmUserDao pmUserDao;
	
	@Resource
	private OrganizationService organizationService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private ShopService shopService;
	@Resource
	private PmUserListService pmUserListService;
	
	 
	
	@Override
	public PmUserListDao getEntityDao() {
		return pmUserListDao;
	}
	/**
	 * 添加是 获得人员
	 * @param user
	 * @return
	 */
	public Page<PmUserList> addUser(Long id, int pageNo, int pageSize){
		Criteria criteria = pmUserListDao.createCriteria();
		criteria.createCriteria("pmUser").add(Restrictions.eq("id",id));
		return findPage(criteria, pageNo, pageSize);
	}

	/**
	 * 获得人员
	 * @param user
	 * @return
	 */
	public List<PmUserList> openAddUserPage(User user,Long[] ObjId, int pageNo, int pageSize){
		PmUser pmUser =new PmUser();
		pmUser.setAuthor(user);
		pmUser.setDeliveryTime(new Date());
		pmUser.setState(PmUserStype.NOTSTART);
		
		List<User> users = null;
		List<PmUserList> pmUserLists = new ArrayList<PmUserList>();
		
		Page<Organization> orPage = null;//查询所有的店
		if(user.getPost().equals(PostType.Admin)){//管理员
			orPage = organizationService.findPageUser(1, 10000);
		}else{
			if(ObjId.length == 0){
				return null;
			}
			orPage = organizationService.findPageUser(ObjId, 1, 10000);
		}
		if(null != orPage){
			List<Organization> organizations = orPage.getResult();
			if(organizations.size()>0){
				for(int i=0;i<organizations.size();i++){
					users = new ArrayList<User>();
					PmUserList pmUserList = new PmUserList();
					Organization organization = organizations.get(i);
					Shop shop = shopService.find(organization.getObjectId());
					System.out.println(shop.getId());
					List<Organization> oList = organizationService.findUserShopByShopId(shop.getId());//根据店铺的ID 查询人员
					for(int j=0;j<oList.size();j++){
						Organization org = oList.get(j);
						System.out.println(org.getName());
						System.out.println(org.getObjectId());
						User us = userService.find(org.getObjectId());
						System.out.println(us.getRealName());
						users.add(us);
					}
					pmUserList.setOrganization(organization.getParent());
					pmUserList.setPmUser(pmUser);
					pmUserList.setUsers(users);
					pmUserList.setShop(shop);
					pmUserLists.add(pmUserList);
				}
			}
		}
		
		return pmUserLists;
	}
	public Boolean yiBoolean(Long id,Date date,User user){
		Criteria criteria = pmUserListDao.createCriteria();
		if(!user.getPost().equals(PostType.Admin)){//判断是否为管理员
			criteria.createCriteria("organization").add(Restrictions.eq("id", id));
		}
		criteria.createCriteria("pmUser").add(Restrictions.between("deliveryTime", DateUtil.getFirstdayLastdayMonthOne(date),DateUtil.getFirstdayLastdayMonth(date))).add(Restrictions.eq("state", PmUserStype.SUBMITED));
//		criteria.createCriteria("pmUser").add(Restrictions.eq("state", PmUserStype.SUBMITED));
		List<PmUserList> pmUserLists = criteria.list();
		if(pmUserLists.size()>0){
			return false;
		}else{
			return true;
		}
	}
	
	
//	public List<PmUserList> findbyMonthDate(String monthDate) {
//		return this.findByMonthDate(monthDate, null);
//	}
	
	public List<PmUserList> findByMonthDate(String monthDate, List<? extends Object> shops) {
		return pmUserListDao.findbyMonthDate(monthDate, shops);
	}
	
	public List<PmUserList> findByMonthDate(String monthDate, Shop shop) {
		return pmUserListDao.findbyMonthDate(monthDate, shop);
	}	
}
